//
//  WebsiteCell.m
//  BaoVn
//
//  Created by Le Anh Tai on 12/20/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "WebsiteCell.h"

#define NORMAL_COLOR [UIColor clearColor]
#define SELECTED_COLOR COLOR_HEX(0x2c2c2c)

@implementation WebsiteCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //_websiteView = [[FavouriteWebsiteView alloc] init];
        //[self.contentView addSubview:_websiteView];
        self.backgroundView = nil;
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        
        _backgroundColorView = [[UIView alloc] init];
        _backgroundColorView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_backgroundColorView];
        
        _logoView = [[UIImageView alloc] init];
        _logoView.contentMode = UIViewContentModeScaleAspectFill;
        _logoView.clipsToBounds = YES;
        _logoView.layer.borderColor = NORMAL_COLOR.CGColor;
        _logoView.layer.borderWidth = 1.0f;
        _logoView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_logoView];
        
        _coverView = [[UIView alloc] init];
        _coverView.backgroundColor = [UIColor blackColor];
        _coverView.alpha = 0.35;
        [self addSubview:_coverView];
        
        _favouriteImageView = [[UIImageView alloc] init];
        [_favouriteImageView setImage:[UIImage imageNamed:@"icon_favorite.png"]];
        [_favouriteImageView setHighlightedImage:[UIImage imageNamed:@"icon_favorite_dis.png"] ];
        [self addSubview:_favouriteImageView];
        
        [self hideFavouriteImage:YES];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

- (void)hideFavouriteImage:(BOOL)hidden
{
    _favouriteImageView.hidden = hidden;
    _coverView.hidden = hidden;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    if (selected)
    {
        _logoView.layer.borderColor = SELECTED_COLOR.CGColor;
        _backgroundColorView.backgroundColor = SELECTED_COLOR;
    }
    else
    {
        _logoView.layer.borderColor = NORMAL_COLOR.CGColor;
        _backgroundColorView.backgroundColor = NORMAL_COLOR;
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    
    if (highlighted)
    {
        _logoView.layer.borderColor = SELECTED_COLOR.CGColor;
        _backgroundColorView.backgroundColor = SELECTED_COLOR;
    }
    else
    {
        if (self.selected == NO)
        {
            _logoView.layer.borderColor = NORMAL_COLOR.CGColor;
            _backgroundColorView.backgroundColor = NORMAL_COLOR;
        }
    }
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    [self hideFavouriteImage:!editing];
    
    if ([_wesiteObject valueForKey:MARKED_WEBSITE_KEY] == nil)
    {
        self.markFavourite = NO;
    }
    else
    {
        self.markFavourite = YES;
    }

}

- (void)setMarkFavourite:(BOOL)markFavourite
{
    _markFavourite = markFavourite;
    
    _favouriteImageView.highlighted = !_markFavourite;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSInteger marginLeft = LOGO_MARGIN;
    NSInteger marginTop = marginLeft/2;
    BOOL isEditting = self.isEditing;
//    _websiteView.frame = self.contentView.bounds;
    
//    if (isEditting)
//    {
//        _logoView.frame = RECT(marginLeft, marginTop, self.width - marginLeft*2 - 50, self.height - marginTop*2);
//    }
//    else
    {
        _logoView.frame = RECT(marginLeft, marginTop, LOGO_WID, self.height - marginTop*2);
    }
    
    _backgroundColorView.frame = self.bounds;
    
    _favouriteImageView.size = SIZE(_logoView.height - marginTop*2, _logoView.height - marginTop*2);
    _favouriteImageView.center = _logoView.center;
    _coverView.frame = _logoView.frame;
}

- (void)loadData:(NSManagedObject *)data
{
    _wesiteObject = data;
//    [_websiteView loadData:data];
    NSString *icon = [data valueForKey:ICON_KEY];
    NSManagedObject *marked = [data valueForKey:MARKED_WEBSITE_KEY];
    
    [_logoView setImage:[UIImage imageNamed:icon]];
    [self setMarkFavourite:(marked != nil)];
}

@end
